package com.flute.tools.xml.xpath;

import java.util.HashSet;

import com.flute.tools.util.StringUtil;

public class PathUtils {
	private static HashSet<Character> validCharacters;

	static {
		validCharacters = new HashSet<Character>();
		for (char c = 'a'; c <= 'z'; c++) {
			validCharacters.add(c);
		}

		for (char c = 'A'; c <= 'Z'; c++) {
			validCharacters.add(c);
		}

		validCharacters.add('-');
		validCharacters.add('_');

		for (char c = '0'; c <= '9'; c++) {
			validCharacters.add(c);
		}
	}

	@SuppressWarnings("unused")
	private static void addCharacters(String string) {
		if (string != null) {
			for (char c : string.toCharArray()) {
				validCharacters.add(c);
			}
		}
	}

	public static void checkPathName(String name) throws XmlPathException {
		if (!StringUtil.isNullOrEmpty(name)) {
			char[] cs = name.toCharArray();
			for (char c : cs) {
				if (!validCharacters.contains(c)) {
					throw new XmlPathException(
							"path contains invalid character '" + c + "'");
				}
			}
		} else {
			throw new XmlPathException("path can not be empty.");
		}
	}
}
